package com.zjsru.leetcode75.level1;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: CookLee
 * @Date: 2022/10/30
 * 最长回文串
 *
 * 输入:s = "abccccdd"
 * 输出:7
 * 解释:
 * 我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
 *
 */
public class LongestPalindrome {
    
    public int longestPalindrome(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (char c : s.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        int res = 0, odd = 0;
        for (Map.Entry<Character, Integer> kv : map.entrySet()) {
            // 将当前字符出现次数向下取偶数，并计入 res
            int count = kv.getValue();
            int rem = count % 2;
            res += count - rem;
            // 若当前字符出现次数为奇数，则将 odd 置 1
            if (rem == 1) {
                odd = 1;
            }
        }
        return res + odd;
    }
    
    public static void main(String[] args) {
        LongestPalindrome longestPalindrome = new LongestPalindrome();
        String s = "abccccdd";
        System.out.println(longestPalindrome.longestPalindrome(s));
    }
}
